package com.efast.dmom.mes.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.dmom.mes.bean.MbomRsBom;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.dao.entity.MbomRsBomDao;
import com.efast.dmom.mes.datahandlers.base.PartRelateDataHandler;
import com.efast.dmom.mes.entity.MbomRsBomEntity;
import com.efast.dmom.mes.util.BaseEntityDataUtil;

@Component
public class MbomRsBomDataHandler extends PartRelateDataHandler<MbomRsBom>{
	@Autowired
	private MbomRsBomDao mbomRsBomDao;
	
	@Override
	public ResultBean save(MbomRsBom obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomRsBomEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.setBaseEntityData(entity);
		int save = mbomRsBomDao.save(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean update(MbomRsBom obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomRsBomEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.updateBaseEntityData(entity);
		int save = mbomRsBomDao.update(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean delete(MbomRsBom obj) {
		ResultBean resultBean = new ResultBean();
		if(obj==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater is null");
			return resultBean;
		}
		MbomRsBomEntity entity = obj.getInnerData();
		if(entity==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("paramater innerdata is null");
			return resultBean;
		}
		
		BaseEntityDataUtil.updateBaseEntityData(entity);
		int save = mbomRsBomDao.delete(entity);
		if(save>0) {
			resultBean.setSuccess(true);
		}else {
			resultBean.setSuccess(false);
			resultBean.setMessage("insert fail");
		}
		return resultBean;
	}

	@Override
	public ResultBean saveBatch(List<MbomRsBom> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean updateBatch(List<MbomRsBom> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ResultBean deleteBatch(List<MbomRsBom> obj) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public MbomRsBom getById(long id) {
		MbomRsBomEntity mbomRsBomEntity = mbomRsBomDao.getById(id);
		if(mbomRsBomEntity!=null) {
			MbomRsBom mbomRsBom = new MbomRsBom();
			mbomRsBom.setInnerData(mbomRsBomEntity);
			return mbomRsBom;
		}
		return null;
	}
	
	
	public List<MbomRsBom> getByMbomId(long mbomId){
		List<MbomRsBomEntity> byMbomId = mbomRsBomDao.getByMbomId(mbomId);
		
		if(byMbomId!=null && byMbomId.size()>0) {
			List<MbomRsBom> list = new ArrayList<MbomRsBom>();
			for(MbomRsBomEntity entity:byMbomId) {
				MbomRsBom mbomRsBom = new MbomRsBom();
				mbomRsBom.setInnerData(entity);
				list.add(mbomRsBom);
			}
			return list;
		}
		return null;
	}

}
